package com.hjy.demo.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjy.demo.pojo.Role;
import com.hjy.demo.pojo.User;
import com.hjy.demo.service.RoleService;
import com.hjy.demo.service.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;



    @RequestMapping("/list")
    public String list(Model model, String usrName,
                       @RequestParam(required = false, defaultValue = "0") Long roleId,
                       @RequestParam(required = false, defaultValue = "1") Integer pageIndex) {

        Page<User> page = new Page<>(pageIndex,5);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (usrName != null && !usrName.equals("")) {
            wrapper.like("usr_name", usrName);
        }
        if (roleId != 0) {
            wrapper.eq("usr_role_id", roleId);
        }
        IPage<User> iPage = userService.selectPage(page, wrapper);

        List<Role> roles = roleService.findAll();
        model.addAttribute("pages", iPage);
        model.addAttribute("roles", roles);

        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        return "user/list";
    }

    @GetMapping("/add")
    public String getadd(Model model){
        List<Role> roleList=roleService.findAll();
        model.addAttribute("roleList",roleList);
        return "/user/add";
    }

    @RequestMapping("/save")
    public String add(Model model,User user){
        if (user!=null){
            if(userService.addUser(user)>0){
                return "redirect:/user/list";
            }
        }
        return "/user/add";
    }

    @GetMapping("/edit")
    public String edit(Model model,Long usrId){
        User user=userService.getUser(usrId);
        List<Role> roleList=roleService.findAll();
        model.addAttribute("roleList",roleList);
        model.addAttribute("user2",user);
        return "/user/edit";
    }

    @RequestMapping("/updateSave")
    public String edit(Model model,User user){
        if (user!=null){
            if(userService.updateUser(user)>0){
                return "redirect:/user/list";
            }
        }
        return "redirect:/user/edit?usrId="+user.getUsrId();
    }

    @GetMapping("/del")
    @ResponseBody
    public Object del(Long usrId){

        return userService.deleteUser(usrId)>0;
    }

}
